package com.minis.aop;

import com.minis.beans.exception.BeansException;
import com.minis.beans.factory.FactoryBean;
import com.minis.beans.factory.annotation.Autowired;
import com.minis.beans.factory.support.BeanFactory;
import com.minis.util.ClassUtils;

/**
 * 根据 target 动态生成一个代理返回
 */
public class ProxyFactoryBean implements FactoryBean<Object>{
    // 用于创建 JdkDynamicAopProxy
    private AopProxyFactory aopProxyFactory;
    private String[] interceptorNames;
    private String targetName;
    // 通过配置的方式注入真正的对象
    private Object target;
    private ClassLoader proxyClassLoader = ClassUtils.getDefaultClassLoader();
    private Object singletonInstance;

    @Autowired
    private BeanFactory beanFactory;

    // 拦截器
    private String interceptorName;
    private PointcutAdvisor advisor;

    public ProxyFactoryBean() {
        this.aopProxyFactory = new DefaultAopProxyFactory();
    }


    // getter, setter
    public void setBeanFactory(BeanFactory beanFactory) {
        this.beanFactory = beanFactory;
    }
    public BeanFactory getBeanFactory() {
        return beanFactory;
    }

    public void setAopProxyFactory(AopProxyFactory aopProxyFactory) {
        this.aopProxyFactory = aopProxyFactory;
    }
    public AopProxyFactory getAopProxyFactory() {
        return this.aopProxyFactory;
    }
    public void setInterceptorName(String interceptorName) {
        this.interceptorName = interceptorName;
    }
    public void setTargetName(String targetName) {
        this.targetName = targetName;
    }
    public Object getTarget() {
        return target;
    }
    public void setTarget(Object target) {
        this.target = target;
    }


    // 核心方法
    @Override
    public Object getObject() throws Exception {
        initializeAdvisor();
        return getSingletonInstance();
    }

    private synchronized void initializeAdvisor() {
        Object advice = null;
//        MethodInterceptor mi = null;
        try {
            // 拿到的是 NameMatchMethodPointcutAdvisor
            advice = this.beanFactory.getBean(this.interceptorName);
        } catch (BeansException e) {
            e.printStackTrace();
        }

//        // 判断增强的类型（Interceptor）
//        if (advice instanceof BeforeAdvice) {
//            mi = new MethodBeforeAdviceInterceptor((MethodBeforeAdvice)advice);
//        }
//        else if (advice instanceof AfterAdvice){
//            mi = new AfterReturningAdviceInterceptor((AfterReturningAdvice)advice);
//        }
//        else if (advice instanceof MethodInterceptor) {
//            mi = (MethodInterceptor)advice;
//        }
//
//        advisor = new DefaultAdvisor();
//        advisor.setMethodInterceptor(mi);
        this.advisor = (PointcutAdvisor) advice;

    }

    private synchronized Object getSingletonInstance() {
        if (this.singletonInstance == null) {
            this.singletonInstance = getProxy(createAopProxy());
        }
        System.out.println("返回代理对象: " + this.singletonInstance.getClass());

        return this.singletonInstance;
    }

    protected Object getProxy(AopProxy aopProxy) {
        return aopProxy.getProxy();
    }

    // 创建动态代理
    protected AopProxy createAopProxy() {
        System.out.println("创建 AopProxy for : " + target);
        return getAopProxyFactory().createAopProxy(target, this.advisor);
    }


    @Override
    public Class<?> getObjectType() {
        // TODO Auto-generated method stub
        return null;
    }

}